package com.zdzx.controller.admin.base;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zdzx.common.result.Result;
import com.zdzx.common.result.ResultCode;
import com.zdzx.common.result.ResultUtil;
import com.zdzx.mapper.DicMapper;
import com.zdzx.model.service.base.vo.Dic;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "dic")
@RestController
@RequestMapping("/admin")
public class DicController {
	
	
	@Autowired
	private DicMapper dicMapper;
	@ApiOperation(value = "查询", notes = "") 
	@GetMapping("/dic") 
	public Result<List<Dic>> findAll(@RequestParam(value = "tenantId", required = false)String tenantId){
		QueryWrapper<Dic> queryWrapper=new QueryWrapper<Dic>();
		queryWrapper.eq("tenant_id",StringUtils.isEmpty(tenantId)?"00000000":tenantId);
		List<Dic> list = dicMapper.selectList(queryWrapper);
		List<Dic> data=new ArrayList<Dic>();
		
		getData(list, data);
		List<Dic> collect = data.stream().sorted(Comparator.comparing(Dic::getIndex)).collect(Collectors.toList());
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null,collect);
	}
	
	private List<Dic> getData(List<Dic> list,List<Dic> data){
		if(data.size()==0) {
		for(int i=0;i<list.size();i++) {
			if("0".equals(list.get(i).getType())) {
				data.add(list.get(i));
				list.remove(list.get(i));
			}
		}
		}
		for(int i=0;i<data.size();i++) {
			for(int j=0;j<list.size();j++) {
				if(data.get(i).getCode().equals(list.get(j).getType())) {
					Dic dic = list.get(j);
					if(!data.get(i).getChildren().contains(dic)) {
					data.get(i).getChildren().add(dic);
					getData(list,data.get(i).getChildren());
					}
				}
			}
		}
		
		return data;
	}
	

	@ApiOperation(value = "添加", notes = "") 
	@PostMapping("/dic") 
	public Result<String> save(@RequestBody Dic entity){
		if(StringUtils.isEmpty(entity.getCode())) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "类标识不能为空！！！", null);
		}
		if(StringUtils.isEmpty(entity.getValue())) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "类名不能为空！！！", null);
		}
		QueryWrapper<Dic> queryWrapper=new QueryWrapper<Dic>();
		queryWrapper.eq("code", entity.getCode());
		List<Dic> selectList = dicMapper.selectList(queryWrapper);
		if(selectList!=null&&selectList.size()>0) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "类标识不能重复！！！", null);
		}
		int rows = dicMapper.insert(entity);
		return ResultUtil.data(rows>0?ResultCode.RESULT_SUCCESS.getCode():ResultCode.RESULT_ERROR.getCode(), null, null);
	}
	
	@ApiOperation(value = "修改", notes = "") 
	@PutMapping("/dic") 
	public Result<String> edit(@RequestBody Dic entity){
		if(StringUtils.isEmpty(entity.getCode())) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "类标识不能为空！！！", null);
		}
		if(StringUtils.isEmpty(entity.getValue())) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "类名不能为空！！！", null);
		}
		QueryWrapper<Dic> queryWrapper=new QueryWrapper<Dic>();
		queryWrapper.eq("code", entity.getCode());
		entity.setCode(null);
		int rows = dicMapper.updateById(entity);
		return ResultUtil.data(rows>0?ResultCode.RESULT_SUCCESS.getCode():ResultCode.RESULT_ERROR.getCode(), null, null);
	}
	
	@ApiOperation(value = "删除", notes = "") 
	@DeleteMapping("/dic/{dicId}") 
	public Result<String> del(@PathVariable("dicId")String dicId){
		Dic selectById = dicMapper.selectById(dicId);
		if(selectById==null) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "", null);
		}
		QueryWrapper<Dic> queryWrapper=new QueryWrapper<Dic>();
		queryWrapper.eq("type", selectById.getCode());
		List<Dic> selectList = dicMapper.selectList(queryWrapper);
		if(selectList!=null&&selectList.size()>0) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "请先删除当前类下的子类！！！", null);
		}
		int rows = dicMapper.deleteById(dicId);
		return ResultUtil.data(rows>0?ResultCode.RESULT_SUCCESS.getCode():ResultCode.RESULT_ERROR.getCode(), null, null);
	}
}
